package com.example.demo.jdk11;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Function;

/**
 * @program: demo
 * @description: 针对var变量
 * @author: zhouhongtao
 * @happyCreateTime: 2021/10/27 14:12
 */
public class VarDemo {


    public static void main(String[] args) {
        var a = "k";
//        a = "a"; // 编译器检查异常
        System.out.println(a);
    }

    public void test(){
        var x = 1.0 / 2.0;
        var list = new ArrayList<String>();
        var stream = list.stream();

        // Type inference can be (Java 8)
        // 使用lamuda语法实现方法helloFunction
        Function helloFunction = s -> "Hello " + s;

        // Inference of generics (Diamond Operator, since Java 7)
        // 泛型推理
        List strings = new ArrayList<>();
        strings.add(helloFunction.apply("World"));
        strings.add(1);
        // Inference of generics (Diamond Operator) with anonymous inner classes (Java 9 -> JEP 213)
        // 使用匿名内部类推断泛型
        Consumer printer = new Consumer<>() {
            /**
             * Performs this operation on the given argument.
             *
             * @param o the input argument
             */
            @Override
            public void accept(Object o) {
                System.out.println(o);
            }

//            @Override
//            public void accept(String string) {
//                System.out.println(string);
//            }
        };
        strings.forEach(printer::accept);
    }



}
